<template>
  <view>
    <view class="btn">
      <button type="primary" @click="goClockIn">账户登录</button>
    </view>
    <view class="btn">
      <button type="primary" @click="withLogin">微信登录</button>
    </view>
  </view>
</template>

<script>
import { getLoginInfo } from "@/api/api.js";

export default {
  data() {
    return {};
  },
  components: {},
  computed: {},
  created() {
    // console.log("created");
    // const urlParams = new URLSearchParams(window.location.search);
    // const code = urlParams.get("code");
    // const projectAppId = urlParams.get("projectAppId");
    // if (code) {
    //   console.log("code", code);
      // this.getLoginInfo(code); // 微信登录
    //   return;
    // }
  },
  onLoad(options) {
    // 获取 URL 中的项目 ID
    if (options.projectId) {
      this.projectAppId = options.projectId;
      // 将 projectAppId 缓存到本地
      uni.setStorageSync("projectAppId", this.projectAppId);
    } else {
      this.projectAppId = uni.getStorageSync("projectAppId");
    }
    console.log("projectAppId", this.projectAppId);
    if (!this.projectAppId) {
      uni.showToast({ title: "参数错误", icon: "none" });
      return;
    }

    const urlParams = new URLSearchParams(window.location.search);
    const code = urlParams.get("code");

    console.log("code", code);
    // let code = options.code;
    // console.log("options", options);
    if (code) {
      console.log("onLoadcode", code);
      this.getLoginInfos(code); // 微信登录
      return;
    } else {
      let userId = uni.getStorageSync("userId");
      if (userId) {
        this.userId = userId;
      } else {
        console.log("未登录，跳转到登录页面");
        // this.loginWithWeixin(); // 微信登录
        // this.withLogin();
      }
    }
  },
  methods: {
    withLogin() {
      // const appId = "wx1b7623b369686483"; // 替换为你的微信开放平台网站应用 AppID
      const appId = "wx9f531b5e4d85c09f"; // 替换为你的微信开放平台网站应用 AppID  观和的appid
      const redirectUri = encodeURIComponent(
        // `https://my.aoejdm.com/api/wechat/serve`
        // `http://gdh6.aoejdm.top`
        `https://gdh6.aoejdm.top/#/pages/login/wx?projectAppId=${this.projectAppId}`
      ); // 替换为你的授权回调域名及路径

      const scope = "snsapi_base"; // 授权作用域，可选 snsapi_base 或 snsapi_userinfo  snsapi_login
      const state = "STATE";
      const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
      console.log("url", url);
      window.location.href = url;
    },
    loginWithWeixin() {
      const appId = "wx50fb1094db86e16b"; // 替换为你的微信开放平台网站应用 AppID
      const redirectUri = encodeURIComponent(
        `http://gdh6.aoejdm.top/#/pages/login/wx?projectAppId=${this.projectAppId}`
      ); // 替换为你的授权回调域名及路径
      const scope = "snsapi_login"; // 授权作用域，可选 snsapi_base 或 snsapi_userinfo  snsapi_login
      const state = "STATE";
      const url = `https://open.weixin.qq.com/connect/qrconnect?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
      console.log("url", url);
      window.location.href = url;
    },
    // 微信登录
    handleLogin() {
      if (uni.getSystemInfoSync().platform === "mp-weixin") {
        uni.login({
          provider: "weixin",
          success: (res) => {
            const code = res.code;
            console.log("code", code);
          },
          fail: (err) => {
            uni.showToast({ title: "微信登录失败", icon: "none" });
          },
        });
      } else {
        console.log("当前平台不支持微信登录", uni.getSystemInfoSync().platform);
        uni.showToast({ title: "当前平台不支持微信登录", icon: "none" });
      }
    },
    // 检查登录状态
    async getLoginInfos(code) {
      try {
        const res = await getLoginInfo({
          code: code,
        });
        console.log("getcoderes", res);
        if (res.code === 1) {
          //微信登录成功，获取用户信息

          uni.setStorageSync("token", res.data.auth);
          uni.setStorageSync("userId", res.data.id);
          uni.setStorageSync("userInfo", res.data);
          uni.switchTab({
            url: `/pages/home/index`,
          });
          return;
        } else {
          uni.showToast({ title: "登录失败", icon: "none" });
        }
      } catch (err) {
        console.log("getLoginInfo", err);
        uni.showToast({ title: "网络请求失败..", icon: "none" });
      }
    },
    goClockIn() {
      uni.navigateTo({
        url: "/pages/login/index",
      });
    },
  },
};
</script>

<style scoped>
/* 图片响应式样式 */

.btn {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  margin-bottom: 10px;
  button {
    height: 40px;
    width: 200px;
    /* margin-right: 10px; */
    /* margin-left: 10px; */
    text-align: center; /* 文字水平居中 */
    line-height: normal; /* 确保文字垂直居中 */
    display: flex; /* 使用 flex 布局 */
    justify-content: center; /* 水平居中 */
    align-items: center; /* 垂直居中 */
  }
}
</style>
